package com.sduept.nwld.dataserver.manager.etl;

import com.sduept.cim.model.core.Substation;
import com.sduept.core.dao.SuperDAO;
import com.sduept.generator.ObjectId;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.bigdata.fault.common.ETLDAO;
import com.sduept.nwld.dataserver.util.CimUtils;
import com.sduept.utils.CharacterUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class EMSStationETLManager {
	@Autowired
	private SuperDAO dao;
	
	@Autowired
	private ETLDAO eDao;
	@Autowired
	private CimManager cm;
	
	public  void receiveAndSaveAllData() {
		List<?> list = getAllSourceData();
		receiveAndSave(list);
	}
	public  void receiveAndSavePartData(String date) {
		List<?> list = getAllSourceData(date);
		receiveAndSave(list);
	}
	
	public static Map<String, String> substationTypeMap = new HashMap<>();
	static {
		substationTypeMap.put("水电厂", "hydro");
		substationTypeMap.put("燃煤发电厂", "thermal");
		substationTypeMap.put("变电站", "trans");
		substationTypeMap.put("风电场", "wind");
	}
	
	public List<?> getAllSourceData(){
		List <?>result = eDao.query("select mrid,name,longitude,latitude,volttype,type from t_ems_substation where volttype >499 ");
		return result;
	}
	public List<?> getAllSourceData(String date) {
		String sql = "select mrid,name,longitude,latitude,volttype,type from t_ems_substation"
				+ " where update_time=";
		List<?> result = eDao.query(sql + "'" + date + "'");
		return result;
	}
	public  void receiveAndSave(List<?> list) {
		ETLDataSave equipmentSave = new ETLDataSave(list);
		equipmentSave.saveToBean();
	}
	/**
	 * 判断s2 是否是s1 的缩写
	 * @param s1
	 * @param s2
	 * @return
	 */
	private  boolean isContain(String s1,String s2){
		char [] stringArr = s2.toCharArray();
		int pos[]=new int[stringArr.length];
		for(int i=0;i<stringArr.length;i++){
			String c = String.valueOf(stringArr[i]);
			if(s1.contains(c)){
				pos[i]=s1.indexOf(c);
				if(i!=0){
					if(pos[i]-pos[i-1]<0){
						return false;
					}
				}
			}else{
				return false;
			}
		}
		return true;
	}
	public class ETLDataSave{
		private List<?> list;
		private Map<String,Substation> map=new HashMap<String,Substation>();
 
		public ETLDataSave(List<?> list) {
			super();
			this.list = list;
			List<Substation> allStations = cm.getAllStations();
			for(Substation sub:allStations){
				if(StringUtils.isNotEmpty(sub.getName())){
					map.put(CharacterUtil.qj2bj(sub.getName()), sub);
				}
			}
		}
		
		public void saveToBean(){
			//select mrid,name,longitude,latitude,volttype,type from t_ems_substation
			List<Substation> addSubList=new ArrayList<Substation>();
			for(Object  obj:list){
				Substation sub=new Substation();
				Object[] objs=(Object[])obj;
				if(objs[0]!=null){
					sub.setId(objs[0].toString());
				}else{
					sub.setId(ObjectId.get().toString());
				}
				if(objs[1]!=null)sub.setName(objs[1].toString());
				if(objs[2]!=null)sub.setLongitude((Double)objs[2]);
				if(objs[3]!=null)sub.setLatitude((Double)objs[3]);
				if(objs[4]!=null){
					sub.setActualVoltage(Float.parseFloat(objs[4].toString()));
					sub.setStandVoltagelevel(CimUtils.getStandVoltagelevel(sub.getActualVoltage()));
				}
				if(objs[5]!=null){
					String type = substationTypeMap.get(objs[5].toString());
					if(null == type) type = "other";
					sub.setSubstationType(type);//
				} 
				Substation substation = map.get(CharacterUtil.qj2bj(sub.getName()));
				if(substation==null){
					boolean isExist=false;
					for(String s:map.keySet()){
						if(isContain(s, CharacterUtil.qj2bj(sub.getName()))||isContain( CharacterUtil.qj2bj(sub.getName()),s)){
							isExist=true;
							break;
						}
					}
					if(!isExist){
						addSubList.add(sub);
					}
				}
			}
			try {
				dao.persistentBatch(Substation.class, addSubList);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
			
	}

}
